﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MayhemCore;
using MayhemCore.ModuleTypes;

namespace MayhemWP7
{
    public partial class Configure : PhoneApplicationPage
    {
        #region Properties

        public Connection Connection
        {
            get { return (Connection)GetValue(ConnectionProperty); }
            set { SetValue(ConnectionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Connection.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ConnectionProperty =
            DependencyProperty.Register("Connection", typeof(Connection), typeof(Configure), new PropertyMetadata(null));

        public bool Enable
        {
            get { return (bool)GetValue(EnableProperty); }
            set { SetValue(EnableProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Enable.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EnableProperty =
            DependencyProperty.Register("Enable", typeof(bool), typeof(Configure), new PropertyMetadata(null));

        // Contains the index this connection is in the list
        // needed for telling the configuration pages what to configure
        private int ConnectionIndex { get; set; }
        #endregion


        public Configure()
        {
            InitializeComponent();

            // Set the data context to use this classes properties in bindings.
            this.DataContext = this;
        }


        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // Get the index of the connection in the connection list
            ConnectionIndex = Int32.Parse(NavigationContext.QueryString["id"]);

            Connection = Mayhem.Instance.ConnectionList[ConnectionIndex];

            Enable = Connection.Enabled;

            // If we started enabled, disable ourselves.
            if (Enable)
            {
                Connection.Disable();
            }
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            // If it was originally enabled and it isn't null (because we deleted it)
            if (Enable && Connection != null)
            {
                // then re-enable it.
                Connection.Enable();
            }
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            Connection.Disable();
            // delete the connection (we are already disabled since we are in this page)
            Mayhem.Instance.ConnectionList.Remove(Connection);

            // remove the reference to the connection so we don't re-enable it
            Connection = null;

            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }  
        }

        private void EventConfig_Click(object sender, RoutedEventArgs args)
        {
            IWp7Configurable e = (IWp7Configurable)Connection.Event;
            Uri uri = e.Wp7Config();

            // Add the index to the uri string
            Uri newUri = new Uri(uri.OriginalString + String.Format("?id={0}", ConnectionIndex), UriKind.RelativeOrAbsolute);

            NavigationService.Navigate(newUri);
        }

        private void ReactionConfig_Click(object sender, RoutedEventArgs e)
        {
            IWp7Configurable reaction = (IWp7Configurable)Connection.Reaction;
            Uri uri = reaction.Wp7Config();

            // Add the index to the uri string
            Uri newUri = new Uri(uri.OriginalString + String.Format("?id={0}", ConnectionIndex), UriKind.RelativeOrAbsolute);

            NavigationService.Navigate(newUri);
        }
    }
}